<template>
	<uni-forms ref="formRef" :model="formData" :rules="formRules" :label-width="150" label-position="top">
		<uni-forms-item label="课程学年" name="schoolYear" required>
			<uni-data-select v-model="formData.schoolYear" :localdata="listType"
				placeholder="请选择课程学年"></uni-data-select>
		</uni-forms-item>
		<uni-forms-item label="备注">
			<uni-easyinput v-model="formData.comment" type="textarea" autoHeight placeholder="请输入备注" trim />
		</uni-forms-item>
	</uni-forms>
</template>

<script lang="ts" setup>
	import { getUser, getDictTypeList } from "@/utils/storage"
	import { gradeEdit, gradeAdd } from "@/api/user"
	import { ref, onBeforeMount } from 'vue'
	const emit = defineEmits(["formCheck"])
	let listType = ref([])
	let userData = ref({})
	const props = defineProps({
		formData: {
			type: Object,
			default: function () {
				return {};
			}
		}
	})
	const formRules = ref({
		schoolYear: {
			rules: [{
				required: true,
				errorMessage: '请选择课程学年'
			}]
		}
	})
	let formRef = ref(null)

	onBeforeMount(() => {
		userData.value = getUser()
		getDictTypeList('SCHOOL_YEAR_TYPE').then(res => { listType.value = res })
	})
	function formCheck() {
		formRef.value.validate().then(res => {
			let id = props.formData.id
			if (id) {
				editOk()
			} else {
				addOK()
			}
		}).catch(err => {
			uni.showToast({ icon: "none", title: err[0].errorMessage })
		})
	}
	function editOk() {
		gradeEdit({
			...props.formData
		}).then(res => {
			if (res.code == '200') {
				uni.$emit('editUpdate', props.formData)
				uni.navigateBack({
					delta: 1
				})
			}
		})
	}
	function addOK() {
		gradeAdd({
			jobseekerId: userData.value.userId,
			...props.formData
		}).then(res => {
			if (res.code == '200') {
				uni.$emit('addUpdate', res.data)
				uni.redirectTo({
					url: './add-upload?itemID=' + res.data.id
				});
			}
		})
	}
	defineExpose({
		formCheck
	})
</script>

<style lang="scss" scoped>
	
</style>